/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import base.*;
import trackmsg.EchoMsg;
import trackmsg.TrackCtrlMsg;

/**
 *
 * @author Mr.Black
 */
public class ASTReleaseExecution extends WaveExecution{

    public ASTReleaseExecution(WaveHeadParser parser, WaveEnvironment waveEnv){
        super(parser, waveEnv);
    }

    public void execute(ASTRelease rl, String tail) {

        Utilities.WPPrint("Release--------------Da chay den day!!!!!!!!!!!!!!");
        Utilities.WPPrint("Wave trong Release: " + rl.getWave());
        Utilities.WPPrint("Tail di kem sau: " + tail);
        Wave releasedWave = new Wave();
        releasedWave.setWaveString(rl.getWave());        
        releasedWave.setWaveEnv(waveEnv.clone());
        releasedWave.getWaveEnv().setTrackAddress(null);
        Utilities.WPPrint("Nhet released wave nay vao trong wave queue: " + rl.getWave());
        waveQueue.incoming(releasedWave);

        if (waveEnv.getTrackAddress() != null && tail.equals("")) {
            //Neu tail ma rong thi gui echo la TRUE di
            TrackNode fromTrack = wi.getTrackForest().getTrackNode(waveEnv.getTrackAddress());
            TrackNode.Echo echo = TrackNode.Echo.TRUE;
            //Tao message echo va gui vao TrackQueue
            TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
            wi.getTrackQueue().incoming(msg);
        }

        // neu tail van con thi thuc thi tiep
        if (!tail.equals("")) {
            Wave remainWave = new Wave(tail);
            remainWave.setWaveEnv(waveEnv);
            Utilities.WPPrint("Nhet tail nay vao trong wave queue: " + tail);
            waveQueue.incoming(remainWave);
        }

        waveEnv.dump();
        Utilities.WPPrint("Release execution Done!");

    }

}
